package Type_2_linked.common;


//单链表
public class ListNode {

    public int val;
    public ListNode next;

    public ListNode() {
    }

    public ListNode(int val) {
        this.val = val;
    }


    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    //打印链表
    public static void printListNode(ListNode head) {
        StringBuilder sb = new StringBuilder("[");
        ListNode current = head;

        while (current != null) {
            sb.append(current.val);
            if (current.next != null) {
                sb.append(", ");
            }
            current = current.next;
        }

        sb.append("]");
        System.out.println(sb);
    }


    //创建链表
    public static ListNode createLinkedList(int[] arr) {
        if (arr == null || arr.length == 0) {
            return null;
        }

        ListNode dummy = new ListNode(0);  //TODO 临时起点
        ListNode current = dummy;

        for (int num : arr) {
            current.next = new ListNode(num);  //TODO 添加新节点
            current = current.next;            //TODO 移动指针(移动到下一个节点)
        }

        return dummy.next;  //TODO 返回临时节点的下一个节点
    }
}
